<template>
	<view class="page">
		<!-- Loading 提示 -->
		<view v-if="loading" class="loading">正在加载...</view>
		
		<!-- 错误提示 -->
		<view v-if="error" class="error">{{ error }}</view>
		
		<!-- 消息列表 -->
		<view v-for="(item, index) in users" :key="index" class="list-item" @click="connect(item)">
			<view class="avatar">
				<text class="round" v-if="item.read"></text>
				<image :src="`http://localhost:8080${item.avatar}`" mode="widthFix"></image>
			</view>
			<view class="content">
				<view class="title">
					<text class="name">{{ item.nickName }}</text>
					<!-- <text class="text">{{item.message}}</text> -->
					<text class="time">{{ formatTimestamp(item.timestamp) }}</text>
				</view>
				<view class="txt">{{ item.message }}</view>
			</view>
		</view>
	</view>
</template>

<script>
import { getMsgList } from "@/api/msg/msg.js";
export default {
	data() {
		return {
			userId: this.$store.state.user.userId,
			users: [], // 消息列表
			loading: true, // 加载状态
			error: null, // 错误信息
		};
	},
	mounted() {
		this.queryMsgList();
	},
	
	onShow() {
		this.queryMsgList();
	},
	
	methods: {
		// 点击联系人
		connect(item) {
			uni.navigateTo({
				url: `/pages/message/chat?name=${item.nickName}&avatar=${item.avatar}&userId=${item.userId}`
			});
		},
		// 查询聊天列表
		queryMsgList() {
			if (!this.userId) {
				this.loading = false;
				this.error = "用户 ID 不存在，请重新登录";
				return;
			}

			getMsgList(this.userId)
				.then((res) => {
					this.loading = false;
					if (res.code === 200) {
						this.users = res.data; // 替换静态数据
					} else {
						this.error = res.message || "加载失败，请稍后重试";
					}
				})
				.catch((err) => {
					this.loading = false;
					this.error = "请求出错，请检查网络连接";
					console.error("请求错误：", err);
				});
		},
		     formatTimestamp(isoTimestamp) {
		         if (!isoTimestamp) return '';
		         const date = new Date(isoTimestamp);
		         const year = date.getFullYear();
		         const month = String(date.getMonth() + 1).padStart(2, '0');
		         const day = String(date.getDate()).padStart(2, '0');
		         const hours = String(date.getHours()).padStart(2, '0');
		         const minutes = String(date.getMinutes()).padStart(2, '0');
		         
		         // 修改时间格式为 YYYY/MM/DD HH:mm
		         return `${year}/${month}/${day} ${hours}:${minutes}`;
		     }



	},
};
</script>

<style lang="scss" scoped>
.page {
	padding: 0 32rpx;
	color: #333;
}
.loading, .error {
	text-align: center;
	color: #999;
	margin-top: 20rpx;
	font-size: 28rpx;
}
.list-item {
	display: flex;
	padding: 30rpx 0;
	border-bottom: 1px solid #ccced3;

	.avatar {
		width: 90rpx;
		height: 90rpx;
		border-radius: 10rpx;
		margin-right: 20rpx;
		position: relative;

		.round {
			position: absolute;
			width: 14rpx;
			height: 14rpx;
			border-radius: 50%;
			background: #ef5656;
			top: -4rpx;
			right: -4rpx;
			z-index: 1;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10rpx;
		}
	}

	.content {
		flex: 1;

		.title {
			display: flex;
			justify-content: space-between;

			.name {
				font-weight: bold;
			}
			

			.time {
				color: #999;
				font-size: 24rpx;
			}
		}

		.txt {
			margin-top: 10rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-line-clamp: 1;
			-webkit-box-orient: vertical;
			text-align: left;
			color: #999;
			font-size: 26rpx;
		}
	}
	
}
</style>
